home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1EUKSZ6 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  10.0 KB  |  314 lines

  1. package com.ibm.uvm.awt.beaninfo;
  2.  
  3. import java.beans.BeanDescriptor;
  4. import java.beans.BeanInfo;
  5. import java.beans.EventSetDescriptor;
  6. import java.beans.FeatureDescriptor;
  7. import java.beans.IntrospectionException;
  8. import java.beans.Introspector;
  9. import java.beans.MethodDescriptor;
  10. import java.beans.ParameterDescriptor;
  11. import java.beans.PropertyDescriptor;
  12. import java.beans.SimpleBeanInfo;
  13. import java.lang.reflect.Method;
  14. import java.text.MessageFormat;
  15. import java.util.ResourceBundle;
  16.  
  17. public class IvjBeanInfo extends SimpleBeanInfo {
  18.    private static ResourceBundle resabtabi = ResourceBundle.getBundle("com/ibm/uvm/awt/beaninfo/abtabi");
  19.    public static final String BOUND = "bound";
  20.    public static final String CONSTRAINED = "constrained";
  21.    public static final String PROPERTYEDITORCLASS = "propertyEditorClass";
  22.    public static final String READMETHOD = "readMethod";
  23.    public static final String WRITEMETHOD = "writeMethod";
  24.    public static final String DISPLAYNAME = "displayName";
  25.    public static final String EXPERT = "expert";
  26.    public static final String HIDDEN = "hidden";
  27.    public static final String PREFERRED = "preferred";
  28.    public static final String SHORTDESCRIPTION = "shortDescription";
  29.    public static final String CUSTOMIZERCLASS = "customizerClass";
  30.    public static final String INDEFAULTEVENTSET = "inDefaultEventSet";
  31.    public static final String ENUMERATIONVALUES = "enumerationValues";
  32.    public static final String ISCONTAINER = "isContainer";
  33.    public static final String CONTAINERDELEGATE = "containerDelegate";
  34.    public static final String OBSCURE = "ivjObscure";
  35.    public static final String DESIGNTIMEPROPERTY = "ivjDesignTimeProperty";
  36.  
  37.    private static String capitalize(String s) {
  38.       if (s.length() == 0) {
  39.          return s;
  40.       } else {
  41.          char[] chars = s.toCharArray();
  42.          chars[0] = Character.toUpperCase(chars[0]);
  43.          return new String(chars);
  44.       }
  45.    }
  46.  
  47.    public BeanDescriptor createBeanDescriptor(Class cls, Object[] args) {
  48.       Class customizerClass = null;
  49.  
  50.       for(int i = 0; i < args.length; i += 2) {
  51.          if ("customizerClass".equals((String)args[i])) {
  52.             customizerClass = (Class)args[i + 1];
  53.             break;
  54.          }
  55.       }
  56.  
  57.       BeanDescriptor bd = new BeanDescriptor(cls, customizerClass);
  58.  
  59.       for(int i = 0; i < args.length; i += 2) {
  60.          String key = (String)args[i];
  61.          Object value = args[i + 1];
  62.          this.setFeatureDescriptorValue(bd, key, value);
  63.       }
  64.  
  65.       return bd;
  66.    }
  67.  
  68.    public EventSetDescriptor createEventSetDescriptor(Class cls, String name, Object[] args, MethodDescriptor[] lmds, Class listenerType, String addListenerName, String removeListenerName) {
  69.       EventSetDescriptor esd = null;
  70.       Class[] paramTypes = new Class[]{listenerType};
  71.  
  72.       try {
  73.          Method addMethod = null;
  74.          Method removeMethod = null;
  75.  
  76.          try {
  77.             addMethod = cls.getMethod(addListenerName, paramTypes);
  78.          } catch (Exception var14) {
  79.             this.throwError(var14, MessageFormat.format(resabtabi.getString("Cannot_get_the_meth1"), addListenerName));
  80.          }
  81.  
  82.          try {
  83.             removeMethod = cls.getMethod(removeListenerName, paramTypes);
  84.          } catch (Exception var13) {
  85.             this.throwError(var13, MessageFormat.format(resabtabi.getString("Cannot_get_the_meth1"), removeListenerName));
  86.          }
  87.  
  88.          esd = new EventSetDescriptor(name, listenerType, lmds, addMethod, removeMethod);
  89.       } catch (Exception var15) {
  90.          this.throwError(var15, MessageFormat.format(resabtabi.getString("Cannot_create_the_E1"), name));
  91.       }
  92.  
  93.       for(int i = 0; i < args.length; i += 2) {
  94.          String key = (String)args[i];
  95.          Object value = args[i + 1];
  96.          if ("inDefaultEventSet".equals(key)) {
  97.             esd.setInDefaultEventSet((Boolean)value);
  98.          } else {
  99.             this.setFeatureDescriptorValue(esd, key, value);
  100.          }
  101.       }
  102.  
  103.       return esd;
  104.    }
  105.  
  106.    public MethodDescriptor createMethodDescriptor(Class cls, String name, Object[] args, ParameterDescriptor[] params, Class[] paramTypes) {
  107.       MethodDescriptor md = null;
  108.  
  109.       try {
  110.          Method aMethod = null;
  111.  
  112.          try {
  113.             aMethod = cls.getMethod(name, paramTypes);
  114.          } catch (Exception var10) {
  115.             this.throwError(var10, MessageFormat.format(resabtabi.getString("Cannot_get_Method_f1"), name));
  116.          }
  117.  
  118.          if (paramTypes.length > 0) {
  119.             md = new MethodDescriptor(aMethod, params);
  120.          } else {
  121.             md = new MethodDescriptor(aMethod);
  122.          }
  123.       } catch (Exception var11) {
  124.          this.throwError(var11, MessageFormat.format(resabtabi.getString("Cannot_create_Metho"), name));
  125.       }
  126.  
  127.       for(int i = 0; i < args.length; i += 2) {
  128.          String key = (String)args[i];
  129.          Object value = args[i + 1];
  130.          this.setFeatureDescriptorValue(md, key, value);
  131.       }
  132.  
  133.       return md;
  134.    }
  135.  
  136.    private PropertyDescriptor createOtherPropertyDescriptor(String name, Class cls) throws IntrospectionException {
  137.       Method readMethod = null;
  138.       Method writeMethod = null;
  139.       String base = capitalize(name);
  140.       Class[] parameters = new Class[0];
  141.  
  142.       try {
  143.          readMethod = cls.getMethod("is" + base, parameters);
  144.       } catch (Exception var9) {
  145.       }
  146.  
  147.       if (readMethod == null) {
  148.          try {
  149.             readMethod = cls.getMethod("get" + base, parameters);
  150.          } catch (Exception var8) {
  151.             readMethod = findMethod(cls, "get" + base, 0);
  152.          }
  153.       }
  154.  
  155.       if (readMethod == null) {
  156.          writeMethod = findMethod(cls, "set" + base, 1);
  157.       } else {
  158.          parameters = new Class[]{readMethod.getReturnType()};
  159.  
  160.          try {
  161.             writeMethod = cls.getMethod("set" + base, parameters);
  162.          } catch (Exception var7) {
  163.          }
  164.       }
  165.  
  166.       if (readMethod == null && writeMethod == null) {
  167.          throw new IntrospectionException(MessageFormat.format(resabtabi.getString("Cannot_find_the_acc1"), name));
  168.       } else {
  169.          return new PropertyDescriptor(name, readMethod, writeMethod);
  170.       }
  171.    }
  172.  
  173.    public ParameterDescriptor createParameterDescriptor(String name, Object[] args) {
  174.       ParameterDescriptor pd = null;
  175.  
  176.       try {
  177.          pd = new ParameterDescriptor();
  178.       } catch (Exception var7) {
  179.          this.throwError(var7, MessageFormat.format(resabtabi.getString("Cannot_create_Param1"), name));
  180.       }
  181.  
  182.       ((FeatureDescriptor)pd).setName(name);
  183.  
  184.       for(int i = 0; i < args.length; i += 2) {
  185.          String key = (String)args[i];
  186.          Object value = args[i + 1];
  187.          this.setFeatureDescriptorValue(pd, key, value);
  188.       }
  189.  
  190.       return pd;
  191.    }
  192.  
  193.    public PropertyDescriptor createPropertyDescriptor(Class cls, String name, Object[] args) {
  194.       PropertyDescriptor pd = null;
  195.  
  196.       try {
  197.          pd = new PropertyDescriptor(name, cls);
  198.       } catch (IntrospectionException var13) {
  199.          try {
  200.             pd = this.createOtherPropertyDescriptor(name, cls);
  201.          } catch (IntrospectionException var12) {
  202.             this.throwError(var12, MessageFormat.format(resabtabi.getString("Cannot_create_the_P1"), name));
  203.          }
  204.       }
  205.  
  206.       this.setFeatureDescriptorValue(pd, "displayName", name);
  207.  
  208.       for(int i = 0; i < args.length; i += 2) {
  209.          String key = (String)args[i];
  210.          Object value = args[i + 1];
  211.          if ("bound".equals(key)) {
  212.             pd.setBound((Boolean)value);
  213.          } else if ("constrained".equals(key)) {
  214.             pd.setConstrained((Boolean)value);
  215.          } else if ("propertyEditorClass".equals(key)) {
  216.             pd.setPropertyEditorClass((Class)value);
  217.          } else if ("readMethod".equals(key)) {
  218.             String methodName = (String)value;
  219.  
  220.             try {
  221.                cls.getMethod(methodName);
  222.             } catch (Exception var11) {
  223.                this.throwError(var11, MessageFormat.format(resabtabi.getString("{0}_no_read_method_"), cls, methodName));
  224.             }
  225.          } else if ("writeMethod".equals(key)) {
  226.             String methodName = (String)value;
  227.  
  228.             try {
  229.                Class type = pd.getPropertyType();
  230.                cls.getMethod(methodName, type);
  231.             } catch (Exception var10) {
  232.                this.throwError(var10, MessageFormat.format(resabtabi.getString("{0}_no_write_method"), cls, methodName));
  233.             }
  234.          } else {
  235.             this.setFeatureDescriptorValue(pd, key, value);
  236.          }
  237.       }
  238.  
  239.       return pd;
  240.    }
  241.  
  242.    public static Method findMethod(Class aClass, String methodName, int parameterCount) {
  243.       try {
  244.          Method[] methods = aClass.getMethods();
  245.  
  246.          for(int index = 0; index < methods.length; ++index) {
  247.             Method method = methods[index];
  248.             if (method.getParameterTypes().length == parameterCount && method.getName().equals(methodName)) {
  249.                return method;
  250.             }
  251.          }
  252.  
  253.          return null;
  254.       } catch (Throwable var6) {
  255.          return null;
  256.       }
  257.    }
  258.  
  259.    public BeanInfo[] getAdditionalBeanInfo() {
  260.       Class superClass = ((SimpleBeanInfo)this).getBeanDescriptor().getBeanClass().getSuperclass();
  261.       if (superClass != null) {
  262.          BeanInfo superBeanInfo = null;
  263.  
  264.          try {
  265.             superBeanInfo = Introspector.getBeanInfo(superClass);
  266.          } catch (IntrospectionException var4) {
  267.             return null;
  268.          }
  269.  
  270.          if (superBeanInfo != null) {
  271.             BeanInfo[] ret = new BeanInfo[1];
  272.             ret[0] = superBeanInfo;
  273.             return ret;
  274.          }
  275.       }
  276.  
  277.       return null;
  278.    }
  279.  
  280.    public int getDefaultEventIndex() {
  281.       return -1;
  282.    }
  283.  
  284.    public int getDefaultPropertyIndex() {
  285.       return -1;
  286.    }
  287.  
  288.    public void handleException(Throwable exception) {
  289.       System.out.println(resabtabi.getString("UNCAUGHT_EXC"));
  290.       exception.printStackTrace(System.out);
  291.    }
  292.  
  293.    private void setFeatureDescriptorValue(FeatureDescriptor fd, String key, Object value) {
  294.       if ("displayName".equals(key)) {
  295.          fd.setDisplayName((String)value);
  296.       } else if ("expert".equals(key)) {
  297.          fd.setExpert((Boolean)value);
  298.       } else if ("hidden".equals(key)) {
  299.          fd.setHidden((Boolean)value);
  300.       } else if ("preferred".equals(key)) {
  301.          fd.setValue(key, value);
  302.       } else if ("shortDescription".equals(key)) {
  303.          fd.setShortDescription((String)value);
  304.       } else {
  305.          fd.setValue(key, value);
  306.       }
  307.  
  308.    }
  309.  
  310.    protected void throwError(Exception e, String s) {
  311.       throw new Error(((Throwable)e).toString() + " " + s);
  312.    }
  313. }
  314.